package routes

import (
	"restapi/handlers"
	"restapi/middlewares"
	"restapi/repositories"
	"restapi/services"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func RegisterRoutes(engine *gin.Engine, db *gorm.DB) {
	// 全局中间件
	engine.Use(middlewares.LoggerMiddleware())
	engine.Use(middlewares.AuthMiddleware())

	// 初始化依赖项
	userHandler := initUserHandler(db)
	studyHandler := initStudyHandler()

	// API分组
	api := engine.Group("/api")
	{
		// 注册各业务模块路由
		RegisterUserRoutes(api, userHandler)

		// 可以继续添加其他业务路由
		RegisterStudyRoutes(api, studyHandler)
		// RegisterOrderRoutes(api, orderHandler)
	}
}

// initUserHandler 初始化用户相关依赖
func initUserHandler(db *gorm.DB) *handlers.UserHandler {
	userRepo := repositories.NewUserRepository(db)
	userService := services.NewUserService(userRepo)
	return handlers.NewUserHandler(userService)
}

func initStudyHandler() *handlers.StudyHandler {
	studyService := services.NewStudyService("Aurora")
	return handlers.NewStudyHanler(studyService)
}
